package data_report

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/analysisClient/rpc"
	"github.com/jinzhu/copier"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ChannelReportLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 渠道报表
func NewChannelReportLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChannelReportLogic {
	return &ChannelReportLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ChannelReportLogic) ChannelReport(req *types.ChanelReportRequest) (resp *types.ChannelReportResponse, err error) {
	resp = &types.ChannelReportResponse{
		Data:     make([]types.ChannelReportData, 0),
		Page:     req.Page,
		PageSize: req.PageSize,
		Total:    0,
	}

	channelAnalysisReq := &rpc.SearchArChannelAnalysisReq{
		StartTime:   req.StartTime,
		EndTime:     req.EndTime,
		ChannelId:   req.ChannelId,
		ChannelName: req.ChannelName,
		Page:        req.Page,
		PageSize:    req.PageSize,
	}
	channelAnalysisResp, err := l.svcCtx.Analysis.SearchArChannelAnalysis(l.ctx, channelAnalysisReq)
	if err != nil {
		return nil, err
	}

	copier.Copy(&resp.Data, channelAnalysisResp.List)
	resp.Total = channelAnalysisResp.Total
	return
}
